package com.wandian.demo.other
{

	import com.wandian.ui.controls.CustomDisplayObject;
	
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.display.Shape;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.text.TextField;
	import flash.utils.Timer;
	import flash.utils.getDefinitionByName;
	
	import utils.Res;
	import utils.UIHelper;
	import utils.events.MenuEvent;
	[SWF(backgroundColor="0x000000", width="800", height="1000")]
	public class test extends Sprite
	{

		public var roolBar1:Sprite=new Sprite();
		public var timer:Timer;
		private var resDomain:ApplicationDomain=new ApplicationDomain(ApplicationDomain.currentDomain);
		private var boolInit:Boolean;
		public var menuArrays:Array=[];
		private var menuSartX:Number=100;
		private var menuSartY:Number=0;
		private var menuContentX:Number=400;
		private var menuContentY:Number=0;
		public var curentContent:MenuItem;
		private var lastMenuX:int;
		private var lastMenuY:Number=0;

		private var count:int=10;
		private var intervalTimer:Number=50;
		private var  betThumbnails:Array=[];
		private var reelsContainer:Sprite=new Sprite();
		private var rancolor:uint=0x000000;
		private var stopflag:int;
		private var reelsCount:int=8;
		private var speedMax:int;
		private var currentBottom:int;
		public function test()
		{
			super();		
		}
		

		protected function onSportTimerHandler(event:TimerEvent):void
		{
			// TODO Auto-generated method stub
			trace(timer.currentCount + ":" + timer.repeatCount);
			var bottom:Sprite=betThumbnails[i] as Sprite;			
			for (var i:int = 0; i < betThumbnails.length; i++) 
			{
				var mysprite:Sprite=betThumbnails[i] as Sprite;
				mysprite.y+=speedMax;
				trace("parent x,y,h,w "+reelsContainer.x+" "+reelsContainer.y+" "+reelsContainer.height+" "+reelsContainer.width);
				trace("child x,y,h,w "+mysprite.x+" "+mysprite.y+" "+mysprite.height+" "+mysprite.width);
			
			}
			for (var h:int = currentBottom; h < betThumbnails.length; h++) {
				if(h>currentBottom){
					
				}
			}
	
			//			for (var j:int = 0; j < betThumbnails.length; j++) 
			//			{
			//				var mysprite:Sprite=betThumbnails[j] as Sprite;
			//				if(mysprite.y
			//			}
			
			
		}
		
		private function onStartClickHandler(event:MouseEvent):void
		{
			// TODO Auto Generated method stub
			startSportTimer();
			stopflag=0;
		}
		private function onStopClickHandler(event:MouseEvent):void
		{
			// TODO Auto Generated method stub
			stopflag=1;
		}
		private function stopSportTimer():void
		{
			// TODO Auto Generated method stub
			trace("Timer stop");
			if (timer != null)
			{
				if (timer.running)
				{
					timer.stop();
					timer.reset();
				}
				timer.removeEventListener(TimerEvent.TIMER, this.onSportTimerHandler);
			}
		}
		
		private function startSportTimer():void
		{
			// TODO Auto Generated method stub
			trace("Timer start");
			if (timer == null)
			{
				timer=new Timer(intervalTimer);
			}
			if (!timer.running)
			{
				timer.addEventListener(TimerEvent.TIMER, onSportTimerHandler);
				timer.start();
			}
		}

		protected function onLoaderRes2CompleteHandler(event:Event):void
		{
			// TODO Auto-generated method stub
			boolInit=true;
			//		  this.addChild(new Bitmap(Res.get("betperline_up")));
			//		  this.addChild(new Bitmap(Res.get("spin_up")));
			//testDynamicBtn();
			//测试按钮
			//testMenu();
			//测试位图点击
//			testBitMap();
			//遮罩文字
			//testTextField();
			//testCustomSprite();
		}
		
		private function testCustomSprite():void
		{
			// TODO Auto Generated method stub
			addChild(new CustomDisplayObject());
		}
		
		private function testTextField():void
		{
			// TODO Auto Generated method stub
			var tf:TextField=new TextField;
			tf.text= "Lorem ipsum dolor sit amet, consectetur adipisicing elit, " 
				+ "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. ";
			tf.selectable=false;
			tf.wordWrap=true;
			tf.width=100;
			addChild(tf);
			var maskSprite:Sprite=new Sprite();
			maskSprite.graphics.beginFill(0xa21223,0.5);
			maskSprite.graphics.drawRect(0,0,100,100);
			addChild(maskSprite);
			tf.mask=maskSprite;
			tf.addEventListener(MouseEvent.MOUSE_DOWN,onDragHandler);
			tf.addEventListener(MouseEvent.MOUSE_UP,onNoDragHandler);
			 function onNoDragHandler(event:MouseEvent):void
			{
				// TODO Auto-generated method stub
				 maskSprite.startDrag();
				
			}
			
			 function onDragHandler(event:MouseEvent):void
			{
				// TODO Auto-generated method stub
				 maskSprite.stopDrag();
				
			}
			
		}
		
	
		
		private function testBitMap():void
		{
			// TODO Auto Generated method stub
			var gun:Bitmap=new Bitmap(Res.get("apple"));
			gun.smoothing=true;
//			gun.width=200;
//			gun.height=200;
			gun.addEventListener(MouseEvent.CLICK, onAppleClickHandler);
			//gun.addEventListener(Event.c, onAppleClickHandler);
			this.addChild(gun);
		}
		
		protected function onAppleClickHandler(event:Event):void
		{
			// TODO Auto-generated method stub
			trace("apple click!");
		}
		
		private function testMenu():void
		{
			// TODO Auto Generated method stub
			this.graphics.beginFill(0x232323);
			this.graphics.drawRect(0, 0, 400, 736);
			this.graphics.endFill();
			//初始化菜单以及对应的显示内容
			initMenu();
			//将菜单绘制到面板
			showMenu();
			this.addEventListener(MenuEvent.onMenuItemClick, onMenuItemClickHandler);
		}

		private function initMenu():void
		{
			// TODO Auto Generated method stub
			menuArrays.push(new MenuItem("menu1", "num1", "num1_over", "apple"));
			menuArrays.push(new MenuItem("menu2", "num2", "num2_over", "banana"));
			menuArrays.push(new MenuItem("menu3", "num3", "num3_over", "pear"));
			menuArrays.push(new MenuItem("menu4", "num4", "num4_over", "watermelon"));
			
		}

		private function showMenu():void
		{
			// TODO Auto Generated method stub
			lastMenuX=0;
			for (var i:int=0; i < menuArrays.length; i++)
			{
				var btn:SimpleButton=MenuItem(menuArrays[i]).menubtn;				
				btn.y=lastMenuY;
				btn.x=this.menuSartX;
				this.addChild(btn);
				lastMenuY+=btn.height;
				//this.addChild(MenuItem(menuArrays[i]).mask);
					//this.addChild(MenuItem(menuArrays[i]).contentbitmap);
			}

		}

		private function registerMenu(menuItem:MenuItem):void
		{
			// TODO Auto Generated method stub			
			if (menuArrays[menuItem.menuItemName] != null)
			{
				return;
			}
			menuArrays[menuItem.menuItemName]=menuItem;
		}

		protected function onMenuItemClickHandler(event:MenuEvent):void
		{
			// TODO Auto-generated method stub
			trace("MenuEvent dispatch");
			this.addEventListener(Event.ENTER_FRAME, onEnterFrameHandler);
		}

		protected function onEnterFrameHandler(event:Event):void
		{
			// TODO Auto-generated method stub

		}

		private function testResTimer():void
		{
			// TODO Auto Generated method stub			
			var loader:Loader=new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaderCompleteHandler);
			var context:LoaderContext=new LoaderContext(false, resDomain);
			loader.load(new URLRequest("res.swf"), context);
		}

		private function testMath():void
		{
			// TODO Auto Generated method stub
			for (var i:int=0; i < 10; i++)
			{
				var num:Number=Math.random();
				trace("num:" + num + ";length:" + num * 10 + ";round:" + Math.round(num * 10));
			}


		}

		protected function onLoaderCompleteHandler(event:Event):void
		{
			// TODO Auto-generated method stub
			trace("load complete");
			event.currentTarget.removeEventListener(Event.COMPLETE, this.onLoaderCompleteHandler);
			initialSprite();
		}

		private function loadRes2():void
		{
			// TODO Auto Generated method stub
			var loader:Loader=new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaderRes2CompleteHandler);
			var context:LoaderContext=new LoaderContext(false, resDomain);
			loader.load(new URLRequest("res3.swf"), context);
		}


		private function testDynamicBtn():void
		{
			// TODO Auto Generated method stub

		}

		private function initialSprite():void
		{
			// TODO Auto Generated method stub		
			//testTimer();
			//初始化玩资源文件，就开始初始化定义样式
			Res.init(resDomain);
			var loader:Loader=new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaderRes2CompleteHandler);
			var context:LoaderContext=new LoaderContext(false, resDomain);
			loader.load(new URLRequest("res3.swf"), context);
		}

		private function testTimer():void
		{
			// TODO Auto Generated method stub
			var btn_start:SimpleButton=new SimpleButton();
			var upshape:Shape=new Shape;
			upshape.graphics.beginFill(0x003232);
			upshape.graphics.drawRect(0, 0, 20, 20);
			upshape.graphics.endFill();
			var downshape:Shape=new Shape;
			downshape.graphics.beginFill(0x000000);
			downshape.graphics.drawRect(0, 0, 20, 20);
			downshape.graphics.endFill();
			btn_start.upState=upshape;
			btn_start.downState=downshape;
			btn_start.overState=upshape;
			btn_start.hitTestState=upshape;
			btn_start.addEventListener(MouseEvent.CLICK, onStartHandler);
			this.addChild(btn_start);

			var btn_sop:SimpleButton=new SimpleButton();
			btn_sop.x=100;
			btn_sop.y=100;
			btn_sop.upState=new Bitmap(Res.get("gun"));
			btn_sop.downState=new Bitmap(Res.get("female"));
			;
			btn_sop.overState=new Bitmap(Res.get("jewelry"));
			;
			btn_sop.hitTestState=new Bitmap(Res.get("knife"));
			;
			btn_sop.addEventListener(MouseEvent.CLICK, onStopHandler);
			this.addChild(btn_sop);
			var gun:Bitmap=new Bitmap(Res.get("gun"));
			gun.smoothing=true;
			gun.width=200;
			gun.height=200;
			this.addChild(gun);

		}

		protected function onStopHandler(event:MouseEvent):void
		{
			// TODO Auto-generated method stub
			trace("stop timer");
			stopTimer();

		}

		private function stopTimer():void
		{
			// TODO Auto Generated method stub
			if (timer != null)
			{
				if (timer.running)
				{
					timer.stop();
				}
				timer.removeEventListener(TimerEvent.TIMER, this.onTimerHandler);
			}
		}

		private function startTimer():void
		{
			// TODO Auto Generated method stub
			if (timer == null)
			{
				timer=new Timer(1000);
			}
			if (!timer.running)
			{
				timer.addEventListener(TimerEvent.TIMER, onTimerHandler);
				timer.start();
			}
		}

		protected function onStartHandler(event:MouseEvent):void
		{
			// TODO Auto-generated method stub
			trace("start timer");
			startTimer();
		}

		protected function onTimerHandler(event:TimerEvent):void
		{
			// TODO Auto-generated method stub
			trace(timer.currentCount + ":" + timer.repeatCount);
		}

		private function testGraphics():void
		{
			// TODO Auto Generated method stub
			roolBar1.graphics.beginFill(0xff0f0f);
			roolBar1.graphics.lineStyle(1, 0x660099);
			roolBar1.graphics.drawRect(100, 100, 200, 241);
			roolBar1.graphics.endFill();
			this.addChild(roolBar1);
		}
	}
}
